package cn.wildfire.chat.app.main;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;
import com.sourcemuch.im.caiba.R2;

import androidx.appcompat.app.AlertDialog;
import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.kit.WfcBaseActivity;
import cn.wildfire.chat.kit.WfcUIKit;
import cn.wildfire.chat.kit.contact.ContactViewModel;
import cn.wildfire.chat.kit.user.SetAliasActivity;
import cn.wildfirechat.model.UserInfo;

/**
 * Create by HEKE, 2019/8/29 20:04
 * TODO 用户信息更多界面
 */
public class UserRemarkMoreActivity extends WfcBaseActivity {

    @BindView(R2.id.setRemark)
    TextView setRemark;
    @BindView(R2.id.deleteFriend)
    TextView deleteFriend;

    private String userId;
    private UserInfo userInfo;

    @Override
    protected int contentLayout() {
        return R.layout.activity_remark;
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        userId = getIntent().getStringExtra("userId");
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 2);
        setReplaceTitle(null, R.string.user_info_more);
        setSearchLayoutVisibility(false);
    }

    @Override
    protected int menu() {
        return R.menu.remark;
    }

    @OnClick({R2.id.setRemark, R2.id.deleteFriend})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.setRemark) {
            remark();
        } else if (id == R.id.deleteFriend) {
            delete();
        }
    }

    /**
     * 跳转至设置备注
     */
    private void remark() {
        Intent intent = new Intent(this, SetAliasActivity.class);
        intent.putExtra("userId", userId);
        startActivity(intent);
    }

    /**
     * 删除好友
     */
    private void delete() {
        try {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle(getString(R.string.friendly_remainder));
            builder.setMessage(getString(R.string.delete_friend_msg));
            builder.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ContactViewModel contactViewModel = WfcUIKit.getAppScopeViewModel(ContactViewModel.class);
                    contactViewModel.deleteFriend(userId).observe(
                            UserRemarkMoreActivity.this, booleanOperateResult -> {
                                if (booleanOperateResult.isSuccess()) {
                                    Intent intent = new Intent(UserRemarkMoreActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    Toast.makeText(UserRemarkMoreActivity.this, getString(R.string.delete_friend_error) + ""
                                            + booleanOperateResult.getErrorCode(), Toast.LENGTH_SHORT).show();
                                }
                            }
                    );
                }
            });
            builder.setNeutralButton(getString(R.string.cancle_but), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            builder.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
